<template>
  <!-- 统计组件，包含滚动数字 -->
  <div class="box">
    <div class="foot-data">
      <div
        class="data-item"
        v-for="item in meetingData"
        :key="item.id">
        <div>
          <div class="number">
            <count-to
              ref='counting'
              :end-val="item.number"
              :duration="3000"
              :suffix="' '+item.unit"
              :autoplay="false">
            </count-to>
          </div>
          <div class="intro">
            {{item.intro}}
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import countTo from 'vue-count-to'
import { totalData } from '@/api/user.js'
export default {
  name: 'Counting',
  components: {
    countTo
  },
  data () {
    return {
      meetingData: [
        {
          id: 1,
          number: 1278,
          unit: '人',
          intro: '用户人数'
        },
        {
          id: 2,
          number: 812,
          unit: '天',
          intro: '安全运行'
        },
        {
          id: 3,
          number: 148,
          unit: '个',
          intro: '覆盖行业和类目'
        },
        {
          id: 4,
          number: 1429,
          unit: '场',
          intro: '会议场次'
        },
        {
          id: 5,
          number: 128,
          unit: '万',
          intro: '为单位/企业节约差旅费用'
        }
      ],
      flag: true
    }
  },
  mounted () {
    window.addEventListener('scroll', this.handleScroll)
    // setInterval(() => {
    this.handleTotalData()
    // }, 2000)
  },
  methods: {
    handleScroll () {
      const scrollTop = window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop
      if (this.flag && scrollTop + document.documentElement.clientHeight > 1400) {
        this.flag = false
        this.$refs.counting.forEach(
          item => item.start()
        )
      }
    },
    /**
     * 获取首页收据
     */
    async handleTotalData () {
      const {
        data: {
          conferenceTotalNum,
          normalRunningDays,
          userTotalNum
        }
      } = await totalData()

      this.meetingData[0].number = userTotalNum
      this.meetingData[1].number = normalRunningDays
      this.meetingData[3].number = conferenceTotalNum
      this.meetingData[4].number = parseInt(1.1 * conferenceTotalNum)
    }
  }
}
</script>

<style lang="scss" scoped>
.box {
  width: 100%;
  background-color: #292929;
}
.foot-data {
  width: $global_width;
  height: 260px;
  background-color: #292929;
  display: flex;
  justify-content: center;
  align-items: center;
  margin: auto;
  .data-item {
    height: 100px;
    width: 230px;
    color: #fff;
    border-right: 1px solid #3c3c3c;
    display: flex;
    align-items: center;
    justify-content: center;
    flex-direction: column;
    text-align: left;
    flex: none;
    box-sizing: border-box;
    &:last-child {
      border-right: 0;
    }
    .number {
      font-weight: 400;
      font-size: 36px;
      margin-bottom: 5px;
    }
    .intro {
      font-size: 14px;
    }
  }
}
</style>
